package cake.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cake.dao.ShoppingCarDao;
import cake.dao.impl.ShoppingCarDaoImpl;
import cake.entity.User;
@WebServlet("/cartedit")
public class CartEditServlet extends HttpServlet {
	private ShoppingCarDao sc=new ShoppingCarDaoImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User user=(User)req.getSession().getAttribute("LoginUser");
		if(user!=null) {
			if(req.getParameter("did")!=null) {
				int did=Integer.parseInt(req.getParameter("did"));
				sc.deleteShoppingCar(user.getId(),did);
			}
			if(req.getParameter("cid")!=null&&req.getParameter("count")!=null) {
				int cid=Integer.parseInt(req.getParameter("cid"));
				int count=Integer.parseInt(req.getParameter("count"));
				sc.updateShoppingCar(count, cid,user.getId());
			}
			if(req.getParameter("id")!=null) {
				int id=Integer.parseInt(req.getParameter("id"));
				if(id==0) {
					sc.deleteShoppingCarAll(user.getId());
				}
			}
		}else {
			resp.sendRedirect("login.html");
		}
	}
}
